/**
 * Copyright (C) 2013 phloc systems
 * http://www.phloc.com
 * office[at]phloc[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phloc.schematron.pure;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;

import org.oclc.purl.dsdl.svrl.SchematronOutputType;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.phloc.commons.io.IReadableResource;
import com.phloc.commons.xml.serialize.XMLReader;
import com.phloc.schematron.AbstractSchematronResource;
import com.phloc.schematron.pure.bound.IPSBoundSchema;
import com.phloc.schematron.pure.bound.PSBoundSchemaCache;
import com.phloc.schematron.pure.bound.PSBoundSchemaCacheKey;
import com.phloc.schematron.svrl.SVRLWriter;

/**
 * A Schematron resource that is not XSLT based but using a native
 * implementation
 * 
 * @author philip
 */
@NotThreadSafe
public class SchematronResourcePure extends AbstractSchematronResource
{
  private final PSBoundSchemaCacheKey m_aKey;

  public SchematronResourcePure (@Nonnull final IReadableResource aResource)
  {
    this (aResource, (String) null);
  }

  public SchematronResourcePure (@Nonnull final IReadableResource aResource, @Nullable final String sPhase)
  {
    super (aResource);
    m_aKey = new PSBoundSchemaCacheKey (aResource, sPhase);
  }

  @Nonnull
  protected IPSBoundSchema getBoundSchema ()
  {
    return PSBoundSchemaCache.getInstance ().getFromCache (m_aKey);
  }

  public boolean isValidSchematron ()
  {
    return getBoundSchema ().getOriginalSchema ().isValid ();
  }

  @Nonnull
  public SchematronOutputType applySchematronValidation (@Nonnull final Node aNode) throws Exception
  {
    return getBoundSchema ().validateComplete (aNode);
  }

  @Override
  @Nullable
  public Document applySchematronValidation (@Nonnull final IReadableResource aXMLResource) throws Exception
  {
    final SchematronOutputType aSO = applySchematronValidationToSVRL (aXMLResource);
    return aSO == null ? null : SVRLWriter.createXML (aSO);
  }

  @Nullable
  public Document applySchematronValidation (@Nonnull final Source aXMLSource) throws Exception
  {
    final SchematronOutputType aSO = applySchematronValidationToSVRL (aXMLSource);
    return aSO == null ? null : SVRLWriter.createXML (aSO);
  }

  @Nullable
  public SchematronOutputType applySchematronValidationToSVRL (@Nonnull final IReadableResource aXMLResource) throws Exception
  {
    if (aXMLResource == null)
      throw new NullPointerException ("XMLResource");

    if (!aXMLResource.exists ())
      return null;

    final Document aDoc = XMLReader.readXMLDOM (aXMLResource);
    if (aDoc == null)
      throw new IllegalArgumentException ("Failed to read resource " + aXMLResource + " as XML");

    return applySchematronValidation (aDoc);
  }

  @Nullable
  public SchematronOutputType applySchematronValidationToSVRL (@Nonnull final Source aXMLSource) throws Exception
  {
    if (aXMLSource == null)
      throw new NullPointerException ("XMLSource");
    if (!(aXMLSource instanceof DOMSource))
      throw new IllegalArgumentException ("Can only handle DOMSource but have " + aXMLSource);

    return applySchematronValidation (((DOMSource) aXMLSource).getNode ());
  }
}
